package com.songlanyun.modules.account.service.impl;

import javax.annotation.Resource;
import javax.annotation.Resources;

import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.*;
import com.songlanyun.modules.account.model.dto.VerifyCodeDTO;
import com.songlanyun.modules.account.service.CommonService;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import java.util.HashMap;
import java.util.Map;


@Service("commonServiceImpl")
public class CommonServiceImpl implements CommonService {

    @Resource
    private RedisUtils redisUtils;
    @Autowired
    private Send smsSend;


    @Value("${token.expiretime}")
    private int EXPIRE;//token过期时间
    @Value("${redis.verCodeTm}")
    private int verCodeTm;  //验证码过期时间
    @Value("${sms.template}")
    private String template = ""; // 短信模板

    @Value("${aliyun.sms.templateCode}")
    private String templateCode ;

    @Resource
    private SmsAliyunUtil smsAliyunUtil;

    @Resource
    private SmsBaoUtil smsBaoUtil;





    @Override
    public boolean getVerfityCode(String mobile) {
        String smsCode = ShareCodeUtils.smsCode();
        boolean send = smsBaoUtil.sendSms(mobile, smsCode);
        if (send) {
            // reids 中不存在 此电话对应验证码证明已经过了 verCodeTm 秒，则可以重新生成
            redisUtils.set(RedisKeys.VERFITY_CODE + mobile, smsCode, verCodeTm);
        }
        return send;
    }

    @Override
    public boolean getVerifyCodeByParam(VerifyCodeDTO dto) {
        String smsCode = ShareCodeUtils.smsCode();
        String mobile = dto.getMobile();
        Integer verifyCodeType = dto.getVerifyCodeType();
        // 参数校验
        verifyParameter(mobile, verifyCodeType);

        boolean send = smsBaoUtil.sendSms(mobile, smsCode);
        if (send) {
            // reids 中不存在 此电话对应验证码证明已经过了 verCodeTm 秒，则可以重新生成
            redisUtils.set(RedisKeys.VERFITY_CODE + mobile, smsCode, verCodeTm);
        }
        return send;
    }


//    @Override
//    public boolean getVerfityCode(String mobile) {
//        // String smsCode = getVerfityCodeService(mobile);
//        String smsCode = ShareCodeUtils.smsCode();
//        //获取短信模板
//        //return sendShortMessage(smsCode, mobile, template);
//        Map<String, Object> param = new HashMap<>();
//        param.put("code",smsCode);
//        String template = JSONUtil.toJsonStr(param);
//        boolean send = sendAliyunSmsMessage(template, mobile, templateCode);
//        if (send) {
//            // reids 中不存在 此电话对应验证码证明已经过了 verCodeTm 秒，则可以重新生成
//            redisUtils.set(RedisKeys.VERFITY_CODE + mobile, smsCode, verCodeTm);
//        }
//        return send;
//    }


//    @Override
//    public boolean getVerifyCodeByParam(VerifyCodeDTO dto) {
//        String smsCode = ShareCodeUtils.smsCode();
//        String mobile = dto.getMobile();
//        Integer verifyCodeType = dto.getVerifyCodeType();
//        // 参数校验
//        verifyParameter(mobile, verifyCodeType);
////        String templateStr = SendTemplate.getTemplateStr(verifyCodeType);
////        if (StrUtil.isBlank(templateStr)) {
////            throw new RRException(StatusMsgEnum.NO_SUCH_SMS_TEMPLATE.getMsg());
////        }
//
//        Map<String, Object> param = new HashMap<>();
//        param.put("code",smsCode);
//        String template = JSONUtil.toJsonStr(param);
//        boolean send = sendAliyunSmsMessage(template, mobile, templateCode);
//        if (send) {
//            // reids 中不存在 此电话对应验证码证明已经过了 verCodeTm 秒，则可以重新生成
//            redisUtils.set(RedisKeys.VERFITY_CODE + mobile, smsCode, verCodeTm);
//        }
//        // 获取短信模板
//        //return sendShortMessage(smsCode, mobile, templateStr);
//        return send;
//    }

    private boolean sendAliyunSmsMessage(String templateParam, String mobile, String templateCode) {
        boolean send = smsAliyunUtil.sendSms(mobile,templateCode,templateParam);
        return send;
    }



    private boolean sendShortMessage(String smsCode, String mobile, String templateStr) {
        String content = templateStr.replace("%1", smsCode);
        // 发送短信
        boolean send = smsSend.send(mobile, content);
        if (send) {
            // reids 中不存在 此电话对应验证码证明已经过了 verCodeTm 秒，则可以重新生成
            redisUtils.set(RedisKeys.VERFITY_CODE + mobile, smsCode, verCodeTm);
        }
        System.out.println("手机验证码----------------" + redisUtils.get(RedisKeys.VERFITY_CODE + mobile));
        return send;
    }

    private void verifyParameter(String mobile, Integer verifyCodeType) {
        if (StrUtil.isBlank(mobile) || ObjectUtil.isNull(verifyCodeType)) {
            throw new RRException(StatusMsgEnum.PARAM_NULL.getMsg());
        }
    }

    @Override
    public void checkVerCode(String mobile, String captcha) {
        String code = redisUtils.get(RedisKeys.VERFITY_CODE + mobile);

        if (code == null) {//redis 已经没有此phone的对应验证码了，
            throw new RRException(StatusMsgEnum.SMSCODESEND_TIMEOUT.getMsg());
        }
        if (!code.equals(captcha)) {//验证码录入错误
            throw new RRException(StatusMsgEnum.SMSCODES_NOMATCH.getMsg());
        }

    }

    /**
     * 生成一个token
     **/
    @Override
    public String createToken(Long userId) {
        String token = TokenGenerator.generateValue();
        redisUtils.set(RedisKeys.getWxTokenConfigKey(token), userId.toString(), EXPIRE);
        return token;
    }


    /**
     * 得到并发送短信验证码
     **/
    public String getVerfityCodeService(String phone) {
        String code = "";
        //reids 中不存在 此电话对应验证码证明已经过了 verCodeTm 秒，则可以重新生成
        code = ShareCodeUtils.smsCode();
        redisUtils.set(RedisKeys.VERFITY_CODE + phone, code, verCodeTm);
        return code;

    }
}
